Skip to content

feat: Discourage assert(Not)Empty if "empty" usage is disallowed - #325

Open
mitelg wants to merge 3 commits into
phpstan:2.0.xfrom
mitelg:feat/dissallow-assert-empty
Open

feat: Discourage assert(Not)Empty if "empty" usage is disallowed#325
mitelg wants to merge 3 commits into
phpstan:2.0.xfrom
mitelg:feat/dissallow-assert-empty

Conversation

@mitelg

@mitelg mitelg commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

fixes: #270

Hey,

first attempt to fix the linked issue. I used Codex for that and I hope it did the job correct 😁 Tests are looking fine for me.

Happy to get some feedback 🤝

Best regards 👋

@mitelg
mitelg force-pushed the feat/dissallow-assert-empty branch 2 times, most recently from 410764b to 927cf58 Compare August 21, 2026 09:45
Comment thread rules.neon Outdated
@mitelg
mitelg force-pushed the feat/dissallow-assert-empty branch from 927cf58 to 812be4d Compare September 10, 2026 14:21
@mitelg
mitelg force-pushed the feat/dissallow-assert-empty branch from 3ac6163 to 589b9ed Compare September 10, 2026 14:31
@mitelg
mitelg requested a review from staabm September 10, 2026 14:56
Comment thread rules.neon
PHPStan\Rules\PHPUnit\AssertEqualsIsDiscouragedRule:
phpstan.rules.rule: [%strictRulesInstalled%, %featureToggles.bleedingEdge%]
PHPStan\Rules\PHPUnit\AssertEmptyIsDiscouragedRule:
phpstan.rules.rule: [%strictRulesInstalled%, %strictRules.disallowedEmpty%]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be also behind %featureToggles.bleedingEdge% ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, I think you are right.

Comment thread rules.neon
@@ -12,6 +12,8 @@ rules:
conditionalTags:
PHPStan\Rules\PHPUnit\AssertEqualsIsDiscouragedRule:
phpstan.rules.rule: [%strictRulesInstalled%, %featureToggles.bleedingEdge%]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@staabm You implemented AssertEqualIsDiscouraged with 10880da

Looking at how AssertEmptyIsDiscouragedRule is configured ; I wonder if it wouldn't be good to be behind disallowedLooseComparison here ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what you are referring to? AssertEqualIsDiscouraged is configured with [%strictRulesInstalled%, %featureToggles.bleedingEdge%]?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean it should be with

[%strictRulesInstalled%, %strictRules.disallowedLooseComparison%, %featureToggles.bleedingEdge%]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we cannot use %strictRules.disallowedLooseComparison% because it would result into a fatal error when strict rules are not installed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%strictRulesInstalled% is a flag which is available because it is known in phpstan-src.

https://github.com/phpstan/phpstan-src/blob/b3547b6e094d604560bd0f97420f2ac8fcfa0961/conf/config.neon#L92

we cannot depend on other 1st party config flags which are only available when a extension is installed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +44 to +50
} elseif ($node instanceof FuncCall) {
if (!$node->name instanceof Name || !in_array(strtolower($scope->resolveName($node->name)), ['phpunit\\framework\\assertempty', 'phpunit\\framework\\assertnotempty'], true)) {
return [];
}
} else {
return [];
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function based assertions seem to be not supported by other Assert* rules, so I think we shouldn't start here.

it would need a separate PR doing it in all rules (not sure its worth adding,... noone asked for these yet AFAIR)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Disallow assertEmpty with strict rule disallowedEmpty set

3 participants